﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;

public partial class KHDangNhapControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] != null)
        {
            btnLogin.Enabled = false;
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["aaa"].ConnectionString);
        SqlCommand cmd = new SqlCommand("select count(*) from KhachHang where Ma_khachhang = @username and Matkhau = @password", con);
        cmd.Parameters.AddWithValue("@username", txtUid.Text);
        cmd.Parameters.AddWithValue("@password", txtPwd.Text);
        con.Open();
        int i = (int)cmd.ExecuteScalar();
        if (i > 0)
        {
            Session["username"] = txtUid.Text;
            Response.Redirect("~/HomePage.aspx");
        }
        else if (txtPwd.Text == "admin" && txtUid.Text == "admin")
        {
            Response.Redirect("~/NVDangNhap.aspx");
        } 
        else
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "CheckLogin", "alert('Username or Password is incorrect !!')", true);
        }
        con.Close();
    }

    public string mahoa(string text)
    {
        MD5CryptoServiceProvider _md5Hasher = new MD5CryptoServiceProvider();
        byte[] bs = Encoding.UTF8.GetBytes(text);
        bs = _md5Hasher.ComputeHash(bs);
        StringBuilder s = new StringBuilder();
        foreach (byte b in bs)
        {
            s.Append(b.ToString("x2"));
        }
        return s.ToString();
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtUid.Text = "";
        txtPwd.Text = "";
    }
}